package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.xinde.XinDeData;
import com.ucredit.babel.common.model.third.xinde.XinDeGrefListRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 信德网贷灰名单
 *
 * @author renzhanzheng
 */

public class XinDeGrefListParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 信德网贷灰名单
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        XinDeGrefListRecord obj = (XinDeGrefListRecord)model;
        XinDeData data = obj.getData();
        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONArray result = info.getJSONArray("result") == null ? new JSONArray() : info.getJSONArray("result");
        JSONArray advanceResult = info.getJSONArray("advanceResult") == null ? new JSONArray() : info.getJSONArray("advanceResult");
        JSONObject ObjResult = result.size() >= 1 ? result.getJSONObject(0) : new JSONObject();
        JSONObject ObjAdvanceResult = advanceResult.size() >= 1 ? advanceResult.getJSONObject(0) : new JSONObject();

        StringBuilder sb = getBaseInfo(obj, successful);
        //任务号
        sb.append(info.getString("tid") == null ? "" : info.getString("tid")).append(CRT001);
        //查询结果状态
        sb.append(info.getString("status") == null ? "" : info.getString("status")).append(CRT001);
        //类型
        sb.append(info.getString("type") == null ? "" : info.getString("type")).append(CRT001);
        //最早借款时间
        sb.append(ObjResult.getString("firstLoanTime") == null ? "" : ObjResult.getString("firstLoanTime")).append(CRT001);
        //历史借款次数
        sb.append(ObjResult.getString("totalLoanCount") == null ? "" : ObjResult.getString("totalLoanCount")).append(CRT001);
        //历史逾期次数
        sb.append(ObjResult.getString("totalOverDueCount") == null ? "" : ObjResult.getString("totalOverDueCount")).append(CRT001);
        //已经还清的历史逾期最长时间
        sb.append(ObjResult.getString("longestOverDuePaid") == null ? "" : ObjResult.getString("longestOverDuePaid")).append(CRT001);
        //当前处于逾期状态的借款笔数
        sb.append(ObjResult.getString("currentOverDueCount") == null ? "" : ObjResult.getString("currentOverDueCount")).append(CRT001);
        //当前逾期总金额
        sb.append(ObjResult.getString("currentOverDueAmount") == null ? "" : ObjResult.getString("currentOverDueAmount")).append(CRT001);
        //当前最长逾期时间(不包括已经还清的)
        sb.append(ObjResult.getString("longestOverDueUnpaid") == null ? "" : ObjResult.getString("longestOverDueUnpaid")).append(CRT001);
        //最后一次申请是否被拒贷
        sb.append(ObjResult.getString("isLastLoanRefused") == null ? "" : ObjResult.getString("isLastLoanRefused")).append(CRT001);
        //最后一次拒贷原因
        sb.append(ObjResult.getString("lastLoanRefuseReason") == null ? "" : ObjResult.getString("lastLoanRefuseReason")).append(CRT001);
        //最后一次拒贷时间
        sb.append(ObjResult.getString("lastLoanRefuseTime") == null ? "" : ObjResult.getString("lastLoanRefuseTime")).append(CRT001);
        //有逾期90天以上运营商联系人个数
        sb.append(ObjAdvanceResult.getString("overDue90ContactsCount") == null ? "" : ObjAdvanceResult.getString("overDue90ContactsCount")).append(CRT001);

        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_xinde_blacklist_inc";
    }

    @Override
    public String getType() {
        return RecordType.XINDE_GREFLIST_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "信德网贷灰名单";
    }
}
